fix: 修复claudecode使用deepseek-v4系列, 上游响应tool_use前没有thinking内容,导致后续对话报错#4597
fix: 修复claudecode使用deepseek-v4系列, 上游响应tool_use前没有thinking内容,导致后续对话报错#4597chi-cat wants to merge 1 commit intoQuantumNous:mainfrom
Conversation
…The `content[].thinking` in the thinking mode must be passed back to the API.), 修复为检测到开启deepseek-v4系列时,自动补充空thiking内容。目前已知deepseek-v4系列默认开启thinking模式(https://api-docs.deepseek.com/zh-cn/quick_start/pricing), 又已知在两个 user 消息之间,如果模型未进行工具调用,则中间 assistant 的 reasoning_content 无需参与上下文拼接,在后续轮次中将其传入 API 会被忽略。在两个 user 消息之间,如果模型进行了工具调用,则中间 assistant 的 reasoning_content 需参与上下文拼接,在后续所有 user 交互轮次中必须回传给 API。 (https://api-docs.deepseek.com/zh-cn/guides/thinking_mode)
WalkthroughA new ChangesDeepSeek V4 Thinking Block Ordering
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~22 minutes Possibly related PRs
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Review rate limit: 7/8 reviews remaining, refill in 7 minutes and 30 seconds.Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@relay/channel/deepseek/thinking.go`:
- Around line 40-43: The prefix check in isDeepSeekV4 is too strict
(HasPrefix("deepseek-v4-")) and mismatches supportsAliAnthropicMessages which
uses Contains("deepseek-v4"); update isDeepSeekV4 to use
strings.HasPrefix(modelName, "deepseek-v4") (or otherwise match the same
"deepseek-v4" substring logic used by supportsAliAnthropicMessages) so models
named "deepseek-v4" and variants like "deepseek-v4-pro" route consistently and
EnsureThinkingBeforeToolUse no longer silently no-ops for the bare alias; adjust
the implementation in isDeepSeekV4 accordingly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: afb17dfd-9f2a-41f2-b8c7-77aaa45868d4
📒 Files selected for processing (3)
relay/channel/ali/adaptor.gorelay/channel/deepseek/adaptor.gorelay/channel/deepseek/thinking.go
| // isDeepSeekV4 checks if the model name belongs to DeepSeek V4 series. | ||
| func isDeepSeekV4(modelName string) bool { | ||
| return strings.HasPrefix(modelName, "deepseek-v4-") | ||
| } |
There was a problem hiding this comment.
isDeepSeekV4 and Ali's supportsAliAnthropicMessages use inconsistent matching strategies.
isDeepSeekV4 requires HasPrefix("deepseek-v4-") (trailing dash), but supportsAliAnthropicMessages uses Contains("deepseek-v4") (no dash). For all current model names (deepseek-v4-flash, deepseek-v4-pro) this is harmless — both checks pass. However, a model configured with the bare alias "deepseek-v4" would be routed through Ali's Anthropic messages path but EnsureThinkingBeforeToolUse would silently no-op, leaving the thinking-before-tool-use gap unfixed.
Consider aligning the two checks:
🛠 Proposed fix
func isDeepSeekV4(modelName string) bool {
- return strings.HasPrefix(modelName, "deepseek-v4-")
+ return strings.HasPrefix(modelName, "deepseek-v4-") || modelName == "deepseek-v4"
}📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| // isDeepSeekV4 checks if the model name belongs to DeepSeek V4 series. | |
| func isDeepSeekV4(modelName string) bool { | |
| return strings.HasPrefix(modelName, "deepseek-v4-") | |
| } | |
| // isDeepSeekV4 checks if the model name belongs to DeepSeek V4 series. | |
| func isDeepSeekV4(modelName string) bool { | |
| return strings.HasPrefix(modelName, "deepseek-v4-") || modelName == "deepseek-v4" | |
| } |
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.
In `@relay/channel/deepseek/thinking.go` around lines 40 - 43, The prefix check in
isDeepSeekV4 is too strict (HasPrefix("deepseek-v4-")) and mismatches
supportsAliAnthropicMessages which uses Contains("deepseek-v4"); update
isDeepSeekV4 to use strings.HasPrefix(modelName, "deepseek-v4") (or otherwise
match the same "deepseek-v4" substring logic used by
supportsAliAnthropicMessages) so models named "deepseek-v4" and variants like
"deepseek-v4-pro" route consistently and EnsureThinkingBeforeToolUse no longer
silently no-ops for the bare alias; adjust the implementation in isDeepSeekV4
accordingly.
修复claudecode使用接口deepseek-v4系列(api.deepseek.com/anthropic容易复现), 上游响应tool_use前没有thinking内容,导致后续对话报错(The
content[].thinkingin the thinking mode must be passed back to the API.),📝 变更描述 / Description
修复为检测到开启deepseek-v4系列时,如果未禁用thinking时,如果assistant消息前没有thiking内容则自动补充空thiking内容。
错误消息体
修复消息体
🚀 变更类型 / Type of change
🔗 关联任务 / Related Issue
#4515
Summary by CodeRabbit